//
//  MKAlertView.m
//  MeetCard
//
//  Created by longmin on 16/11/9.
//  Copyright © 2016年 timtian. All rights reserved.
//

#import "MKAlertView.h"
#import "UIImage+Color.h"
#import "UIImage+SnapShot.h"

@interface MKAlertView ()

@property(nonatomic, strong) UIView *contentView;

@property(nonatomic, strong) UIImageView *imageView;

@property(nonatomic, strong) void (^confirmBlock)();

@property(nonatomic, strong) void (^cancelBlcok)();

@property(nonatomic, strong) NSMutableArray *btnArr;

@property(nonatomic, strong) UIImageView *animationImage;

@end

@implementation MKAlertView

#pragma mark - 生命周期

-(void)dealloc {
    NSLog(@"dealloc---%@", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.detailLabel];
        [self.contentView addSubview:self.imageView];
        [self addSubview:self.animationImage];
    }
    return self;
}

#pragma mark - set方法和get方法

-(NSMutableArray *)btnArr {
    if (_btnArr == nil) {
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

-(UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.width = 250 * __kwRatio;
        _contentView.height = 185 * __kwRatio;
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 1.5;
        _contentView.clipsToBounds = YES;
    }
    return _contentView;
}

-(UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

-(UIImageView *)animationImage {
    if (_animationImage == nil) {
        _animationImage = [[UIImageView alloc] init];
    }
    return _animationImage;
}

-(UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = _kFont32;
        _titleLabel.textColor = kColor3C3C3C;
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        _detailLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

-(UILabel *)detailLabel {
    if (_detailLabel == nil) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = _kFontMain;
        _detailLabel.textColor = kColor3C3C3C;
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        _detailLabel.numberOfLines = 0;
    }
    return _detailLabel;
}

#pragma mark - IBActions

-(void)cancelClick:(id)btn {
    
    if (self.cancelBlcok) {
        self.cancelBlcok();
        self.cancelBlcok = nil;
    }
    [self dissmiss];
}

-(void)confirmClick:(id)btn {
    
    if (self.confirmBlock) {
        self.confirmBlock();
        self.confirmBlock = nil;
    }
    [self dissmiss];
}

#pragma mark - Public

-(void)show {
    
    if (!self.btnArr.count) {   //如果没有添加事件,默认给一个
        
        [self addActionWithType:MKAlertViewTypeDefault title:@"确定" clickBlock:nil];
        
    }
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    CGRect originalFrame = self.contentView.frame;
    
    UIImage *animationImage = [UIImage screenShotWithView:self.contentView forRect:self.contentView.bounds];
    self.animationImage.image = animationImage;
    self.animationImage.alpha = 0.1;

    self.contentView.hidden = YES;
    
    [UIView animateWithDuration:MK_DURATION
                          delay:0
         usingSpringWithDamping:0.8
          initialSpringVelocity:0.618
                        options:UIViewAnimationOptionCurveLinear animations:^{
        
                            self.animationImage.frame = originalFrame;
                            self.animationImage.alpha = 1.0;
                            
                            self.backgroundColor = kRGBACOLOR(0, 0, 0, 0.5);
                            
    } completion:^(BOOL finished) {
        self.contentView.hidden = NO;
        [self bringSubviewToFront:self.contentView];
    }];
    
}

-(void)dissmiss {
    
    self.contentView.hidden = YES;
    
    [self bringSubviewToFront:self.animationImage];
    
    
    [UIView animateWithDuration:MK_DURATION animations:^{
        
        self.animationImage.width *= 0.4;
        self.animationImage.height *= 0.4;
        self.animationImage.center = self.center;
        
        self.animationImage.alpha = 0.4;
        self.backgroundColor = [UIColor clearColor];
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
    
}

-(void)setTitleFont:(UIFont *)titleFont {
    self.titleLabel.font = titleFont;
    [self updateLabel];
}

-(void)setDetailFont:(UIFont *)detailFont {
    self.detailLabel.font = detailFont;
    [self updateLabel];
}

-(void)addActionWithType:(MKAlertViewType)type title:(NSString *)title clickBlock:(void(^)())clickBlock {
    
    LMButtonType btnType = type == MKAlertViewTypeCancel ? LMButtonTypeCancel : LMButtonTypeMove;
    
    LMButton *actionBtn = [LMButton lm_buttonWithType:btnType
                                                title:title];
    if (type == MKAlertViewTypeDefault) {
        
        self.confirmBlock = clickBlock;
        [actionBtn addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
    }else {
        
        UIImage *d9Img = [UIImage lm_imageWithColor:kColorD9D9D9];
        [actionBtn setBackgroundImage:d9Img forState:UIControlStateNormal];
        UIImage *cbImg = [UIImage lm_imageWithColor:kColor4BAACE];
        [actionBtn setBackgroundImage:cbImg forState:UIControlStateHighlighted];
        self.cancelBlcok = clickBlock;
        [actionBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.contentView addSubview:actionBtn];
    
    actionBtn.tag = self.btnArr.count;
    [self.btnArr addObject:actionBtn];
    
    actionBtn.height = 44 * __kwRatio;
    
    
    CGFloat twoCountWid = (self.contentView.width - 2*MK_PADDING_30 - MK_PADDING_20) * 0.5;
    
    for (LMButton *btn in self.btnArr) {
        
        if (self.btnArr.count < 2) {
            btn.width = actionBtn.width = self.contentView.width - 2 * MK_PADDING_30;
            btn.left = MK_PADDING_30;
        }else {
            
            btn.width = twoCountWid;
            btn.left = btn.tag * (MK_PADDING_20 + btn.width) + MK_PADDING_30;
            
        }
        
    }
    
    actionBtn.bottom = self.contentView.height - MK_PADDING_30;
    
}

+(instancetype)alertWithTitle:(NSString *)title detail:(NSString *)detail {
    
    return [self alertWithImage:nil Title:title detail:detail];
}

+(instancetype)alertWithImage:(NSString *)image Title:(NSString *)title detail:(NSString *)detail {
    
    MKAlertView *alertView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertView.backgroundColor = kRGBACOLOR(0, 0, 0, 0.5);
    alertView.titleLabel.text = title;
    alertView.detailLabel.text = detail;
    
    alertView.contentView.centerX = alertView.width * 0.5;
    alertView.contentView.centerY = alertView.height * 0.5;
    
    CGFloat scale = __kScreenWidth / alertView.contentView.width;
    
    alertView.animationImage.frame = alertView.contentView.frame;
    alertView.animationImage.width *= scale;
    alertView.animationImage.height *= scale;
    alertView.animationImage.center = alertView.center;
    
    
    if (image.length) {
        alertView.imageView.image = [UIImage imageNamed:image];
        alertView.imageView.width = 70;
        alertView.imageView.height = 41;
        alertView.imageView.top = 24;
        alertView.imageView.centerX = CGRectGetCenter(alertView.contentView.bounds).x;
    }
    
    [alertView updateLabel];
    
    return alertView;
}

#pragma mark - Private

-(void)updateLabel {
    
    self.detailLabel.size = [self.detailLabel.text boundingRectWithSize:CGSizeMake(210, self.detailLabel.font.lineHeight * 3)
                                                                options:NSStringDrawingUsesLineFragmentOrigin
                                                             attributes:@{NSFontAttributeName : self.detailLabel.font}
                                                                context:nil].size;
    
    CGFloat labelBottom = self.contentView.height - MK_PADDING_30 - 44 * __kwRatio;
    
    if (self.titleLabel.text.length) {
        
        self.titleLabel.size = [self.titleLabel.text boundingRectWithSize:CGSizeMake(210, self.titleLabel.font.lineHeight * 3)
                                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                                 attributes:@{NSFontAttributeName : self.titleLabel.font}
                                                                    context:nil].size;
        
        CGFloat sumH = self.titleLabel.height + self.detailLabel.height + 25;
        
        if (!self.detailLabel.text.length) {
            self.titleLabel.centerY = labelBottom * 0.5;
        }else {
            
            self.titleLabel.top = 15;
            self.detailLabel.bottom = sumH - 20 + 15;
        }
        
        
    }else {
        
        self.detailLabel.centerY = labelBottom * 0.5;
        
    }
    
    if (self.imageView.height) {
        self.titleLabel.top = self.imageView.bottom + MK_PADDING_20;
    }
    
    self.detailLabel.centerX = self.titleLabel.centerX = self.contentView.width * 0.5;
}

#pragma mark - 数据源和代理方法
#pragma mark - UIView -> 下面放继承自UIView的方法

@end
